package com.gemme.gemme.wode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.home.PageAllProductBean;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.shequ.ShequDetailActivity;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.BrandListBottomView;
import com.gemme.gemme.view.RefreshView;
import com.gemme.gemme.view.Topview;
import com.jaeger.library.StatusBarUtil;

import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

//侧滑删除地址
public class ShoucangActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoucang);
        initTop();
        initView();
        getDataS();
    }



    RefreshView refreshView;
    private void initView() {
        refreshView = findViewById(R.id.refreshView);
        refreshView.setLayoutManager(new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL));

    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("收藏的内容");
    }


    public class ShoucangBrandAdapter extends RecyclerView.Adapter<ShoucangBrandAdapter.ViewHolder>{

        private List<CollectBean.DataBean.RecordsBean> adapterList;

        public ShoucangBrandAdapter(List<CollectBean.DataBean.RecordsBean> adapterList) {
            this.adapterList = adapterList;
        }

        @NonNull
        @Override
        public ShoucangBrandAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.shoucang_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new ShoucangBrandAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ShoucangBrandAdapter.ViewHolder holder, int position) {
            try {
                CollectBean.DataBean.RecordsBean recordsBean = adapterList.get(position);
                holder.tv_label.setText(StrUtil.noNUll(recordsBean.getLabel()));
                holder.tv_name.setText(StrUtil.noNUll(recordsBean.getNickName()));
                holder.tv_dianzan.setText(StrUtil.noNUll(recordsBean.getLikeCounts()) + "");
                if("1".equals(recordsBean.getType())){//图片
                    String coverPath = recordsBean.getCoverPath();
                    if(!TextUtils.isEmpty(coverPath))
                    Glide.with(mContext).load(coverPath).into(holder.picture);
                }else {
                    String videoPath = recordsBean.getVideoPath();
                    if(!TextUtils.isEmpty(videoPath)){
                        Glide.with(mContext)
                                .setDefaultRequestOptions(
                                        new RequestOptions()
                                                .frame(1000000)
                                                .centerCrop()
                                )
                                .load(videoPath)
                                .into(holder.picture);

                    }

                }

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int id = recordsBean.getId();
                        if(id < 0){
                            Toast.makeText(mContext, "暂无数据", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Intent intent = new Intent(mContext, ShequDetailActivity.class);
                        intent.putExtra("data",id);
                        startActivity(intent);
                    }
                });


            }catch (Exception e){

            }
        }

        @Override
        public int getItemCount() {
            return adapterList.size();
        }



        public class ViewHolder extends RecyclerView.ViewHolder {
           ImageView picture;
           TextView tv_label;
            TextView tv_name;
            TextView tv_dianzan;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                picture = itemView.findViewById(R.id.picture);
                tv_label = itemView.findViewById(R.id.tv_label);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_dianzan = itemView.findViewById(R.id.tv_dianzan);
            }
        }
    }


    private void getDataS() {
        String url = DataManager.BaseUrl + "community/appcuserslike/collectlist" ;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{

                    CollectBean collectBean = JSON.parseObject(response, CollectBean.class);
                    List<CollectBean.DataBean.RecordsBean> records = collectBean.getData().getRecords();
                    System.out.println("");
                    if(records == null || records.size() == 0){
                        findViewById(R.id.nullView).setVisibility(View.VISIBLE);
                        refreshView.setVisibility(View.GONE);
                        return;
                    }else{
                        findViewById(R.id.nullView).setVisibility(View.GONE);
                        refreshView.setVisibility(View.VISIBLE);
                    }

                    refreshView.setAdapter(new ShoucangBrandAdapter(records));
                }catch (Exception e){
                }
            }
        });
    }


}